<?php

App::uses('CrudController', 'Controller');
App::uses('UserTeam', 'Model');

class UserTeamsController extends CrudController
{
    public $_model = 'UserTeam';

    public function admin($teamId, $lineId, $areaId, $moeId) {
        $this->request->allowMethod('get');
        $this->loadModel('Area');
        $this->loadModel('Moe');
        $this->loadModel('Line');
        $this->loadModel('Team');
        $this->loadModel('Shift');
        $this->loadModel('UserLine');
        $users = $this->UserLine->getByLine($lineId);
        $appBosch = new stdClass();
        $appBosch->teamId = $teamId;
        $appBosch->lineId = $lineId;
        $appBosch->areaId = $areaId;
        $appBosch->moeId = $moeId;
        $appBosch->types = UserTeam::getTypes();
        $appBosch->users = $users;
        $description = $this->getDescription($teamId, $lineId, $areaId, $moeId);
        $this->set('appBosch', $appBosch);
        $this->set('title', __('Integrantes del equipo de trabajo'));
        $this->set('description', $description);
    }

    private function getDescription($teamId, $lineId, $areaId, $moeId) {
        $this->Moe->id = $moeId;
        $moe = $this->Moe->read();
        $this->Area->id = $areaId;
        $area = $this->Area->read();
        $this->Line->id = $lineId;
        $line = $this->Line->read();
        $this->Team->id = $teamId;
        $team = $this->Team->read();
        $this->Shift->id = $team['Team']['shift_id'];
        $shift = $this->Shift->read();
        $parts = array(
            $moe['Moe']['name'],
            $area['Area']['name'],
            $line['Line']['name'],
            $shift['Shift']['name'],
        );
        return implode(' - ', $parts);
    }

    protected function getRecords($param) {
        $m = $this->_model;
        $records = $this->$m->getTeam($param);
        return $records;
    }

    protected function c($model) {
        return array(
            'type' => $model->type,
            'user_id' => $model->user_id,
            'team_id' => $model->team_id,
        );
    }

    protected function u($model) {
        return array(
            'type' => $model->type,
            'user_id' => $model->user_id,
        );
    }

}
